'use strict';

const db = uniCloud.database();
const periodCollection = db.collection('period_records');

exports.main = async (event, context) => {
	// 获取用户ID
	const { userId } = event;
	
	if (!userId) {
		return {
			code: 1,
			message: '缺少用户ID参数'
		}
	}
	
	try {
		// 查询该用户的所有月经记录
		const { data } = await periodCollection
			.where({
				user_id: userId
			})
			.orderBy('date', 'desc')
			.get();
		
		return {
			code: 0,
			message: '获取月经记录成功',
			data: data.map(item => {
				return {
					_id: item._id,
					userId: item.user_id,
					date: item.date,
					symptoms: item.symptoms || [],
					flowLevel: item.flow_level || 1,
					notes: item.notes || '',
					createDate: item.create_date
				}
			})
		}
	} catch (error) {
		console.error('获取月经记录失败:', error);
		return {
			code: 500,
			message: '获取月经记录失败:' + error.message
		}
	}
}; 